import Cookies from "js-cookie"
import { defineStore } from 'pinia'
import type { Store } from './types'
import APIAuth from '@/api/auth/index'
import { useSocket } from '@/stores/index'

export const useAuth = defineStore('auth', {
    state: (): Store => ({
        /** 是否为登录状态 */
        token: '',
        info: {},
        dialogUploadMaterialVisible: false
    }),
    actions: {
        /** 设置登录状态 */
        setToken(token: string): void {
            this.token = token
            this.getInfo()
        },
        /** 获取基本信息 */
        getInfo() {
            const { initSocket } = useSocket()
            APIAuth.getPersonal().then(({ data }) => {
                this.info = data
                initSocket()
            })
        },
        /** 清除登录状态 */
        clearToken(): void {
            this.token = ''
            Cookies.remove('token', { domain: import.meta.env.VITE_APP_COOKIE_URL })
            window.location.href = import.meta.env.VITE_APP_WEB_URL
        },
    }
})
